package com.universest.swordholder.data.repository

import com.universest.swordholder.data.UserDatabase
import com.universest.swordholder.data.dao.LongText
import com.universest.swordholder.data.dao.LongTextDao
import kotlinx.coroutines.flow.Flow

class LongTextRepository(val database: UserDatabase) {

    private val longTextDao: LongTextDao = database.longTextDao()

    fun getTextById(textId: Int): Flow<LongText?> {
        return longTextDao.getTextById(textId)
    }

    fun getAllTexts(): Flow<List<LongText>> {
        return longTextDao.getAllTexts()
    }

    suspend fun newLongText(text: String): LongText{
        val longText = LongText(content = text)
        val id = insert(longText)
        return longText.copy(textId = id.toInt())
    }

    suspend fun insert(longText: LongText): Long {
        return longTextDao.insert(longText)
    }

    suspend fun update(longText: LongText) {
        longTextDao.update(longText)
    }

    suspend fun delete(longText: LongText) {
        longTextDao.delete(longText)
    }

    suspend fun deleteById(textId: Int) {
        longTextDao.deleteById(textId)
    }
}